package com.example.mybatis.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@ControllerAdvice
public class CustomerExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(CustomerExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public void handleException(Exception ex, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf8");
        logger.error("Inner Server Error,Caused by: "+ex.getMessage());
        PrintWriter writer = response.getWriter();
        writer.write("{\"status\":500,\"message\":\"Inner Server Error\"}" );
        writer.flush();
        writer.close();

    }

}
